package com.zzb.service.impl;


import com.zzb.DTO.ResultDTO;
import com.zzb.DTO.UserDTO;
import com.zzb.VO.UserVO;
import com.zzb.dao.UserDao;
import com.zzb.dao.impl.UserDaoImpl;
import com.zzb.entity.UserEntity;
import com.zzb.service.UserService;
import com.zzb.util.Md5;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private UserEntity userEntity = new UserEntity();

    @Override
    public ResultDTO login(UserVO userVO) {
//        判断验证码是否正确
        if (userVO.getCode().equals(userVO.getSessionCode())) {

            UserEntity userEntity = Tool.copy(userVO, UserEntity.class);
            String mingwen = userEntity.getPassword();
            String anwen = Md5.encoderByMd5(mingwen);
            userEntity.setPassword(anwen);

            UserEntity model = userDao.findByUsernameAndPassword(userEntity);
            if (model != null) {
                return ResultDTO.getResult("success", 1);
            } else {
                return ResultDTO.getResult("error", 0);
            }

        } else {
            return ResultDTO.getResult("error-code", -1);
        }
    }

    @Override
    public ResultDTO changePasswordById(UserVO userVO) {
        BeanUtils.copyProperties(userVO, userEntity);
        String oldPassword = userEntity.getOldPassword();
        String password = userEntity.getPassword();
        userEntity = userDao.findById(userEntity);
        if (userEntity.getPassword().equals(Md5.encoderByMd5(oldPassword))) {
            userEntity.setPassword(Md5.encoderByMd5(password));
            return ResultDTO.getResult("success", userDao.update(userEntity));
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }


    @Override
    public ResultDTO add(UserVO userVO) {
        //数据复制
        BeanUtils.copyProperties(userVO, userEntity);
        //判断用户是否存在
        if (userDao.findByName(userEntity) == null) {//不存在
            //加密
            String mingwen = userEntity.getPassword();
            String anwen = Md5.encoderByMd5(mingwen);
            userEntity.setPassword(anwen);
            return ResultDTO.getResult("success", userDao.add(userEntity));
        } else {//用户已存在
            return ResultDTO.getResult("error", 0);
        }

    }

    @Override
    public ResultDTO delete(UserVO userVO) {
        //数据复制
        BeanUtils.copyProperties(userVO, userEntity);
        //判断用户是否存在
        if (userDao.findById(userEntity) != null) {//存在
            return ResultDTO.getResult("success", userDao.delete(userEntity));
        } else {//用户不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(UserVO userVO) {
        BeanUtils.copyProperties(userVO, userEntity);
        userEntity = userDao.findById(userEntity);
        if (userEntity != null) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userEntity, userDTO);
            return ResultDTO.getResult("success", userDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }

    }

    @Override
    public ResultDTO findAll(UserVO userVO) {
        BeanUtils.copyProperties(userVO, userEntity);
        int count = userDao.count(userEntity);
        List<UserEntity> list = userDao.findAll(userEntity);
        List<UserDTO> userDTOList = Tool.copy(list, UserDTO.class);
        return ResultDTO.getResult(count, userDTOList);
    }

    @Override
    public ResultDTO update(UserVO userVO) {
        BeanUtils.copyProperties(userVO, userEntity);
        UserEntity userEntityFindByName = userDao.findByName(userEntity);
        if (userEntityFindByName == null) {//类型名称不存在或者没有修改名字
            return ResultDTO.getResult("success", userDao.update(userEntity));
        }
        UserEntity userEntityFindById = userDao.findById(userEntity);
        String newName = userEntity.getUsername();
        String oldName = userEntityFindById.getUsername();
        if (oldName.equals(newName)) {
            userEntity.setPassword(userEntityFindById.getPassword());
            return ResultDTO.getResult("success", userDao.update(userEntity));
        }
        return ResultDTO.getResult("error", 0);
    }
}
